#include "Items.h"
#include "World/Entity/Local References.h"
#include <fstream>
#include <assert.h>
#include <string>
#include <stdio.h>
//#include <sstream>
#include "World/Entity/Statsheet.h"
using namespace std;
Item_Consumable::Item_Consumable() : Item_Base()
{
	usesLeft = 0;
}


void Item_Consumable::_fileInit(char* FileName, recipe* a_Recipe)
{
	//===BaseObject
	objectType		= TYPE_ENTITY_PIECE;
	itemType		= ITEM_CONSUMABLE;
	active			= true;

	ifstream File(FileName);
	if(File.is_open())
	{
		string trashData;
		string goodData;

		for(int i = 0; i < 4; i++)
		{
			getline(File,trashData,':');
			getline(File,goodData,';');
			a_Recipe->namIngredients[i] = wordModify::copy(a_Recipe->namIngredients[i],goodData);
		}
		//===ItemName
		getline(File,trashData,':');
		getline(File,goodData ,';');
		itemName = wordModify::copy(itemName,goodData);
		//===Skill Alignment
		getline(File,trashData,':');
		getline(File,goodData ,';');
		//skillAlignment = atoi(goodData);
		for(int i = 0; i < STAT_NUMOFSTATS; i++)
			if(statNames[i] == goodData)
				skillAlignment = i;
		//===Skill Requisite
		getline(File,trashData,':');
		getline(File,goodData ,';');
		skillRequisite = atoi(goodData.c_str());
		//===Uses Left
		getline(File,trashData,':');
		getline(File,goodData ,';');
		usesLeft = atoi(goodData.c_str());
		//===Addiction Chance
		int numElems,duration;
		bool isOverTime;
		//===NumEffects
		getline(File,trashData,':');
		getline(File,goodData ,';');
		numElems = atoi(goodData.c_str());
		//===Duration
		getline(File,trashData,':');
		getline(File,goodData ,';');
		duration = atoi(goodData.c_str());
		//===IsOverTime?
		getline(File,trashData,':');
		getline(File,goodData ,';');
		int temp = atoi(goodData.c_str());
		isOverTime = (temp > 0?true:false);

		this->effects.init(numElems,duration,isOverTime, itemName);
		int stat;
		int amount;
		for(int i = 0; i < numElems; i++)
		{
			//===stat
			getline(File,trashData,':');
			getline(File,goodData ,';');
			//stat = wordModify::copy(stat,goodData);
			if(goodData == "PierceDamage")		stat = MSTAT_CURRPIERCE;
			else if(goodData == "ImpactDamage")		stat = MSTAT_CURRIMPACT;
			else if(goodData == "EnergyDamage")		stat = MSTAT_CURRENERGY;
			else if(goodData == "MetaDamage")		stat = MSTAT_META;
			else if(goodData == "ManiaDamage")		stat = MSTAT_CURRMANIA;
			else if(goodData == "AgressionDamage")	stat = MSTAT_CURRAGRESSION;
			else if(goodData == "DisciplineDamage")	stat = MSTAT_CURRDISCIPLINE;
			else
				for(int i = 0; i < STAT_NUMOFSTATS; i++)
				{
					//if(statNames[i] == goodData)
					const char* c_goodData = goodData.c_str();
					if(compare(statNames[i], (char*)c_goodData))
						stat = i;
				}

				//===Amount
				getline(File,trashData,':');
				getline(File,goodData ,';');
				amount = atoi(goodData.c_str());

				this->effects.addbuff(stat,amount);
		}
	}
	File.close();
};
void Item_Consumable::_copyInit(Item_Consumable* toCopy)
{
	this->assetIndex		= toCopy->assetIndex;
	this->objectType		= toCopy->objectType;

	this->usesLeft			= toCopy->usesLeft;
	this->effects			= toCopy->effects;
	this->iconAsset			= toCopy->iconAsset;
	this->itemName			= toCopy->itemName;
	this->itemType			= toCopy->itemType;
	this->skillAlignment	= toCopy->skillAlignment;
	this->skillRequisite	= toCopy->skillRequisite;
	this->uniqueID			= toCopy->uniqueID;
}
/*Entity Side Logic - Going through a Consumable
Notes:
-There is nothing preventing entity from chugging down all the uses to superstack the effect.
*Higher addiction chance
*Worse withdrawls.

When consumed...
-usesLeft decrements
-entity checks against addictionLevel to see if they get addicted
-entity	copies effects to a list within themselves to run through 
until the effect timer wears off

On Entity Update
-Entity iterates through their list of effects
*Within that, they iterate through its list of buffs
if(effects[x].isOverTime)
stats[effects[x].buffs[y].statToAffect] += effects[x].buffs[y].amount 
else
//do nothing but wait

When the Effect wears off
if(!effects[x].isOverTime)
stats[effects[x].buffs[y].statToAffect] -= effects[x].buffs[y].amount 
else
//do nothing
*/